package com.hzq.controller;

import com.hzq.empty.Dept;
import com.hzq.empty.User;
import com.hzq.service.DeptService;
import com.hzq.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门
     * @return
     */
    @GetMapping("/findAll")
    public String findAll(Map map){
        List<Dept> deptList = deptService.findAll();
        map.put("dept",deptList);
        return "deptList";
    }

    @GetMapping("/toadd")
    public String toadd(){
        return "addDept";
    }

    /**
     * 添加部门
     * @param dept
     */
    @PostMapping("/addDept")
    public String addDept(Dept dept){
        this.deptService.addDept(dept.getDeptid(),dept.getDeptname());
        return "redirect:/dept/findAll";
    }

    /**
     * 根据id查询页面信息
     * @param deptid
     * @param map
     * @return
     */
    @GetMapping("/findDeptById/{deptid}")
    public String findDeptById(@PathVariable("deptid") Integer deptid,Map map){
        Dept dept = this.deptService.findDeptById(deptid);
        map.put("dept",dept);
        return "updateDept";
    }

    /**
     * 修改部门信息
     * @param dept
     */
    @PostMapping("/updateDept/{deptid}")
    public String updateDept(@PathVariable("deptid") Integer deptid,String deptname){
        this.deptService.updateDept(deptid,deptname);
        return "redirect:/dept/findAll";
    }

    /**
     * 删除部门信息
     * @param deptid
     */
    @GetMapping("/delDept/{deptid}")
    public String delDept(@PathVariable("deptid") Integer deptid){
        this.deptService.delDept(deptid);
        return "redirect:/dept/findAll";
    }

}
